﻿/**
 * 定义仓储类。
 */
Ext.define('Happy.metadata.template.DefineStore', {
    alias: 'template.define-store',
    requires: [
        'Happy.data.Store',
        'Happy.data.TreeStore'
    ],

    /**
     * 执行运行时代码生成。
     * 
     * @param {Happy.metadata.ClassGenerator} generator
     * @param {Happy.metadata.Metadata} metadata
     */
    generate: function (generator, metadata) {
        var me = this;

        me.defineStore(generator, metadata);
        if (metadata.isTree()) {
            me.defineTreeStore(generator, metadata);
        }
    },

    /**
     * 定义仓储类型。
     * 
     * @private
     * @param {Happy.metadata.ClassGenerator} generator
     * @param {Happy.metadata.Metadata} metadata
     */
    defineStore: function (generator, metadata) {
        var classConfig = {
            extend: 'Happy.data.Store',

            metadata: metadata,

            model: metadata.getModelClassName()
        };
        Ext.merge(classConfig, metadata.data.store || {});
        Ext.define(metadata.getStoreClassName(),classConfig);
    },

    /**
     * 定义树形仓储类型。
     * 
     * @private
     * @param {Happy.metadata.ClassGenerator} generator
     * @param {Happy.metadata.Metadata} metadata
     */
    defineTreeStore: function (generator, metadata) {
        var classConfig = {
            extend: 'Happy.data.TreeStore',

            metadata: metadata,

            model: metadata.getTreeModelClassName()
        };
        Ext.merge(classConfig, metadata.data.treeStore || {});
        Ext.define(metadata.getTreeStoreClassName(), classConfig);
    }
});